<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>    
    <div id="app">      
        <p>age:{{age}}</p>
        <p>isAdult：{{isAdult}}</p>
        <input type="button" value="修改Age" @click="changeAge" >
        
        <p>count:{{count}}</p>
        <p>isLimit:{{isLimit}}</p>
        <p>limitChina:{{limitChina}}</p>
        <input type="button" value="修改Count" @click="changeCount" >
    </div>
</body>
</html>

<script src="./js/vue.global.js"></script>
<script>    

     const {createApp,ref,reactive,watch,watchEffect,computed} = Vue;

    let app = createApp({
        setup(){

            let age = ref(16);
            let isAdult = ref(false);

            let count = ref(5);
            let maxCount = 10;
            let isLimit = ref(false);//是否达到上限
            let limitChina = computed(()=>isLimit.value?"不能再买了":"继续买");

            // 1、watchEffect回调函数里，使用哪个响应式数据，那么就会监听哪个数据。

            watchEffect(()=>{                
                console.log("watchEffect：age.value",age.value);
                // 此处写的是 age发生变化时的副作用（即：当age发生变化，还应该做什么事情）
                isAdult.value = age.value>=18?true:false;

                // 此处写的是：count发生变化时的副作用。
                isLimit.value = count.value>=maxCount?true:false;

            });

            // watch(age,function(){
            //     console.log("watch：age.value",age.value);
            // })

            function changeAge(){
                age.value++;
            }

            function changeCount(){
                
                if(isLimit.value){
                    return;
                }

                count.value++;
            }
            
            return {
                age,changeAge,isAdult,
                count,changeCount,isLimit,limitChina
            }
        }
    })

    app.mount("#app");

</script>